package com.star.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.star.domain.*;
import com.star.mapper.*;
import com.star.service.ShopService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Star
 * @since 2021-04-21
 */
@Service
public class ShopServiceImpl extends ServiceImpl<ShopMapper, Shop> implements ShopService {
    /**
     * 店铺
     */
    @Autowired
    private ShopMapper shopMapper;
    /**
     * 角色
     */
    @Autowired
    private UserRoleMapper userRoleMapper;

    /**
     * 登录信息整合
     */
    @Autowired
    private UserLoginMapper userLoginMapper;
    /**
     * 区域表
     */
    @Autowired
    private AreaMapper areaMapper;

    @Autowired
    private ShopTypeMapper shopTypeMapper;

    /**
     * 查询店铺 分页
     * @param num
     * @param size
     * @return
     */
    @Override
    public IPage<Map<String, Object>> findClientPage(Integer num, Integer size) {
        //创建QueryWrapper搜索对象，判断参数不为空则传入参数
        QueryWrapper<Map<String, Object>> wrapper = new QueryWrapper<>();

        //创建分页对象
        Page<Map<String, Object>> page = new Page<>(num, size);

        return shopMapper.shopFindAll(page, wrapper);
    }

    /**
     * 根据店铺ID查询
     * @param shopId 店铺ID
     * @return 对象
     */
    @Override
    public Shop shopById(Integer shopId){
        return shopMapper.shopById(shopId);
    }

    /**
     * 注册操作
     *
     * @param shop
     * @return
     */
    @Override
    @Transactional
    public boolean shopRegister(Shop shop, UserLogin userLogin, String areaName, ShopType shopType) {
        //将区域进行修改成规定格式
        //如果是四大直辖市中的一个的话 把前面的市去掉
        String[] municipality = {"北京市", "天津市", "上海市", "重庆市"};
        String areaOne = areaName.split(",")[0];
        for (String s : municipality) {
            if (areaOne.equals(s)) {
                areaName = s.substring(0, s.length() - 1) + areaName.substring(3);
                break;
            }
        }
        areaName = "中国," + areaName;
        int shopAdd = shopMapper.insert(shop);


        //修改 用户角色表
        UpdateWrapper<UserRole> updateUserRoleWrapper = new UpdateWrapper<>();
        //查询区域表 对店铺信息进行修改
        QueryWrapper<Area> areaQueryWrapper = new QueryWrapper<>();
        //查询店铺类型表
        QueryWrapper<ShopType> shopTypeQueryWrapper = new QueryWrapper<>();
        //修改登录id
        UpdateWrapper<Shop> shopUpdateWrapper = new UpdateWrapper<>();
        //插入成功
        if (shopAdd > 0) {
            UserRole userRole = new UserRole();
            //用户角色修改 顾客变为商家
            userRole.setRoleId(2);
            updateUserRoleWrapper.eq("log_id", userLogin.getLogId());
            userRoleMapper.update(userRole, updateUserRoleWrapper);
            //查区域表信息
            areaQueryWrapper.eq("MergerName", areaName);
            Area area = areaMapper.selectOne(areaQueryWrapper);
            //进行修改
            shopMapper.shopAreaUpdate(area.getID(), shop.getShopId());
            //获取店铺数据
            shopTypeQueryWrapper.eq("shop_Type_name", shopType.getShopTypeName());
            ShopType shopTypeOne = shopTypeMapper.selectOne(shopTypeQueryWrapper);
            //根据店铺类型ID进行修改
            shopMapper.shopTypeUpdate(shopTypeOne.getShopTypeId(), shop.getShopId());
            //修改登录Id
            shopMapper.logIdUpdate(userLogin.getLogId(),shop.getShopId());
            return true;
        } else {
            throw new RuntimeException("创建店铺失败");
        }
    }

}
